const app = getApp()

const exitLoginBehavior = require('../../behaviors/personal/exitLogin.js')
import {
    validateUsername,
    checkRegNo
} from '../../../../utils/validate.js'
import {
    reqUpdateUser
} from '../../../../request/api.js'
import {
    showMsg
} from '../../../../utils/config.js'
Page({
    behaviors: [exitLoginBehavior],
    data: {
        username:"",
        idCard:"",
        nickname:"",
    },
    onLoad(){
        this.setData({
            nickname: app.globalData.nickname
        })
    },
    handleusername(e){
        this.setData({
            'username': e.detail.value
        })
    },
    usernameClear(event){
        this.setData({
            username: "",
        })
    },
    handleidCard(e){
        this.setData({
            'idCard': e.detail.value
        })
    },
    idCardClear(){
        this.setData({
            idCard: "",
        })
    },
      // 获取昵称
      nicknameChange(e) {
        this.setData({
            nickname: e.detail.value
        })
    },
    // 清除昵称
    clearNickename() {
        this.setData({
            nickname: ''
        })
    },
    backLogin() {
        wx.reLaunch({
            url: '/pages/login/login',
        })
    },
     // 确定
     async bindtapShoudon() {
        if (!validateUsername(this.data.username)) return
        if (this.data.nickname=='') return showMsg('请输入昵称');
        if (!checkRegNo(this.data.idCard)) return
        const res = await reqUpdateUser({
            id: app.globalData.userId,
            username:this.data.username,
            nickname: this.data.nickname,
            idCard:this.data.idCard
        })
        if (res.code != 20000) return showMsg(res.message);
        // showMsg("修改成功")
        app.globalData.username = this.data.username
        wx.setStorageSync('username', this.data.username);
        app.globalData.nickname = this.data.nickname
        wx.setStorageSync('nickname', this.data.nickname);
        app.globalData.idCard = this.data.idCard
        wx.setStorageSync('idCard', this.data.idCard);
        if(app.globalData.rolename=='NORMAL_USER'){
            // 业主不需要选择单位
           app.globalData.isProprietor = true
           wx.setStorageSync('isProprietor', true);
           wx.switchTab({
               url: '/pages/index/index',
           })
       }
    },
})